<?php
/**
 * Created by yb.
 * Author: 1021948372@qq.com
 * Date: 2020/7/7 07:07
 */
namespace app\admin\controller;

use app\common\lib\SessionCheck;

class Ad extends SessionCheck
{

    /**
     * 列表
     * @return mixed
     */
    public function index(){
        //广告位列表
        $positionList = db('ad_position')->select();
        $this->assign('positionList',$positionList);
        return $this->fetch();
    }

    public function getDataList(){
        $map = array();
        if(!empty(input('keyword'))){
            $map[] = ['title','like','%' . input('keyword') . '%'];
        }
        if(input('status') != '-1'){
            $map[] = ['status','=',input('status')];
        }
        if(!empty(input('position_id'))){
            $map[] = ['position_id','=',input('position_id')];
        }
        if(!empty(input('start_date')) && !empty(input('end_date'))){
            $map[] = ['create_time','between',[strtotime(input('start_date')),strtotime(input('end_date')) + 86400]];
        }elseif(!empty(input('start_date'))){
            $map[] = ['create_time','>=',strtotime(input('start_date'))];
        }elseif(!empty(input('end_date'))){
            $map[] = ['create_time','<=',strtotime(input('end_date')) + 86400];
        }
        $result = model('Ad')->getPageListData('*',$map,'create_time desc');
        foreach($result['dataList'] as $k => &$v){
            $v['position_title'] = db('ad_position')->where(array('id' => $v['position_id']))->value('title');
            $v['start_time'] = $v['start_time'] ? date('Y-m-d H:i:s',$v['start_time']) : '不限制';
            $v['end_time'] = $v['end_time'] ? date('Y-m-d H:i:s',$v['end_time']) : '不限制';
        }
        $this->success('操作成功','',$result);
    }

    /**
     * 添加
     * @return mixed
     */
    public function add(){
        if(request()->isPost()){
            if(empty(input('position_id'))){
                $this->error('请选择位置');
            }
            if(empty(input('title'))){
                $this->error('请填写标题');
            }
            if(empty(input('img'))){
                $this->error('请上传图片');
            }
            $saveData = array(
                'position_id' => input('position_id'),
                'title' => input('title'),
                'img' => input('img'),
                'link_url' => input('link_url','','trim'),
                'sort' => input('sort',50,'int'),
                'start_time' => input('start_time') ? strtotime(input('start_time')) : 0,
                'end_time' => input('end_time') ? strtotime(input('end_time')) : 0,
                'status' => input('status',1,'int'),
                'body' => input('body'),
                'create_time' => time()
            );
            $model = new \app\admin\model\Ad();
            if($id = $model->insertGetId($saveData)){
                //如果是链接到当前活动，将该id更新到此数据
                if(input('link_url','','trim') == 'detail'){
                    $model->save(['link_url' => 'pages/addetail/index?detail_id='.$id],['id' => $id]);
                }
                $this->success('操作成功');
            }else{
                $this->error('操作失败');
            }
        }else{
            //位置列表
            $positionList = db('ad_position')->select();
            $this->assign('positionList',$positionList);
            return $this->fetch();
        }
    }

    /**
     * 编辑
     * @return mixed
     */
    public function edit(){
        if(request()->isPost()){
            if(empty(input('id'))){
                $this->error('ID参数缺失');
            }
            if(empty(input('position_id'))){
                $this->error('请选择位置');
            }
            if(empty(input('title'))){
                $this->error('请填写标题');
            }
            if(empty(input('img'))){
                $this->error('请上传图片');
            }

            $saveData = array(
                'position_id' => input('position_id'),
                'title' => input('title'),
                'img' => input('img'),
                'link_url' => input('link_url','','trim'),
                'sort' => input('sort',50,'int'),
                'start_time' => input('start_time') ? strtotime(input('start_time')) : 0,
                'end_time' => input('end_time') ? strtotime(input('end_time')) : 0,
                'status' => input('status',1,'int'),
                'body' => input('body'),
                'update_time' => time()
            );
            $model = new \app\admin\model\Ad();
            if($model->save($saveData,['id' => input('id')])){
                if(input('link_url','','trim') == 'detail'){
                    $model->save(['link_url' => 'pages/addetail/index?detail_id='.input('id')],['id' => input('id')]);
                }
                $this->success('操作成功');
            }else{
                $this->error('操作失败');
            }
        }else{
            $id = input('id',0,'int');
            $data = db('ad')->where(array('id' => $id))->find();
            $data['start_time'] = $data['start_time'] ? date('Y-m-d H:i:s',$data['start_time']) : '';
            $data['end_time'] = $data['end_time'] ? date('Y-m-d H:i:s',$data['end_time']) : '';
            $this->assign('data',$data);
            //位置列表
            $positionList = db('ad_position')->select();
            $this->assign('positionList',$positionList);
            return $this->fetch();
        }
    }

    /**
     * 删除
     */
    public function del(){
        $id = input('id',0,'int');
        $model = new \app\admin\model\Ad();
        if($model->where(array('id' => $id))->delete()){
            $this->success('操作成功');
        }else{
            $this->error('操作失败');
        }
    }

    /**
     * 修改状态
     */
    public function editStatus(){
        $id = input('id',0);
        $status = input('status',1);
        $saveData = array(
            'status' => $status
        );
        $model = new \app\admin\model\Ad();
        if($model->save($saveData,['id'=>$id])){
            $this->success('修改成功');
        }else{
            $this->error('修改失败');
        }
    }

    public function editStatusMany(){
        $ids = input('ids');
        if(empty($ids)){
            $this->error('请选择需要操作的数据');
        }
        $status = input('status',1);
        $saveData = array(
            'status' => $status
        );
        $model = new \app\admin\model\Ad();
        if($model->save($saveData,[['id','in',$ids]])){
            $this->success('修改成功');
        }else{
            $this->error('修改失败');
        }
    }

}